﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Data;
using System.Reflection;

namespace System
{
    public static class ExtensionMethods
    {

        public static void Each<T>(this IEnumerable<T> list, Action<T> action)
        {
            foreach (var item in list) { action(item); }
        }


        public static List<T> ToGenericList<T>(this DataTable datatable, Func<DataRow, T> converter)
        {
            return (from row in datatable.AsEnumerable()
                    select converter(row)).ToList();
        }

        public static DateTime ToDateTime(this object data)
        {
            DateTime result;
            try
            {
                result = System.Convert.ToDateTime(data.ToString());
            }
            catch (Exception)
            {
                result = DateTime.MinValue;
            }
            return result;
        }

        public static DateTime?  ToNullableDateTime(this object data)
        {
            DateTime? result;
            try
            {
                result = System.Convert.ToDateTime(data.ToString());
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }


        public static int ToInteger(this object data)
        {
            int result;
            int.TryParse(data.ToString(), out result);
            return result;
        }

        public static decimal ToDecimal(this object data)
        {
            decimal result;
            decimal.TryParse(data.ToString(), out result);
            return result;
        }

        public static bool ToBoolean(this object data)
        {
            bool result;
            bool.TryParse(data.ToString(), out result);
            return result;
        }

        public static string ToJson(this object data)
        {
            string result = "";
            try
            {
                result = JsonConvert.SerializeObject(data, Formatting.Indented,
                                            new JsonSerializerSettings()
                                            {
                                                ReferenceLoopHandling = ReferenceLoopHandling.Serialize
                                            });
            }
            catch (Exception)
            {
            }
            return result;
        }
        public static T FromJson<T>(this string data)
        {
            T result = default(T);
            try
            {
                result = JsonConvert.DeserializeObject<T>(data);
            }
            catch (Exception)
            {
            }
            return result;
        }

        public static T ToDerived<T>(this object baseclass)
        {
            T derived = (T)Activator.CreateInstance(typeof(T));
            foreach (PropertyInfo propBase in baseclass.GetType().GetProperties())
            {
                try
                {
                    PropertyInfo propDerived = typeof(T).GetProperty(propBase.Name);
                    propDerived.SetValue(derived, propBase.GetValue(baseclass, null), null);
                }
                catch (Exception)
                {

                }
            }
            return derived;
        }
    }
}
